import { Component, ViewChild } from '@angular/core';
import { Events, AlertController, App, ModalController, NavController, Content } from 'ionic-angular';
import { MainService } from './main.service';
import { Storage } from '@ionic/storage';
import { Config } from '../../providers/config';
import { DomSanitizer } from '@angular/platform-browser';
import { EmailDetailPage } from '../email/detail/detail.component';
import { TaskDetailPage } from '../task/detail/detail.component';
import { TaskListService } from '../task/list/list.service';
import { InfoDetailPage } from '../../pages/info/detail/detail.component';
import _ from 'lodash';
import { DraftDetailPage } from "../draft/detail/detail.component";

import { Common } from "../../providers/common";

@Component({
  selector: 'main-page',
  templateUrl: './main.html',
  providers: [MainService, TaskListService]
})
export class MainPage {
  moa_page_name: any = "main-page";
  moa_page_explain: any = "OA首页";

  // 列表数据
  listData: any = {};

  isFromMainPage = true;

  @ViewChild(Content) content: Content;

  // 选中页面

  //defaultSelectePage= 'todo';
  selectedPage = '';
  headerMode = '';

  menus = [{
    id: 'todo',
    name: '办公',
    component: "todo-page"
  }, {
    id: 'usp',
    name: '营销',
    component: "page-marketing-home"
  }, {
    id: 'bcw',
    name: '风控',
    component: "page-bcw-home"
  }];

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public mainService: MainService,
    public taskListService: TaskListService,
    public config: Config,
    private sanitizer: DomSanitizer,
    public events: Events,
    public storage: Storage,
    private common: Common
  ) {
    let that = this;
    that.app['_title'] = '首页';
    this.storage.get("configs").then(configs => {
      let regularMenu: any = _.find(configs, {
        name: "regularMenu"
      })['json'];

      let uspHidden: any = _.find(regularMenu, {
        id: "usp"
      })['list'];

      let isHiddenBcw: boolean = _.find(regularMenu, { id: "bcw" })['isHidden'];
      let isHiddenUsp: boolean = _.find(uspHidden, { id: "usp-yxsy" })['isHidden'];

      if (isHiddenBcw) {
        _.remove(that.menus, {
          id: 'bcw'
        });
      }
      if (isHiddenUsp) {
        _.remove(that.menus, {
          id: 'usp'
        });
      }
      if (that.menus.length == 1) {
        this.headerMode = 'title';
      } else {
        this.headerMode = 'page';
      }
      console.log(that.menus);
    });

    this.storage.get("selected-home-page").then(function (page) {
      if (page) {
        that.selectedPage = page;
      } else {
        that.selectedPage = 'todo';
      }
      //检测默认进来的页面，是否需要添加水印
      that.common.addWatermark({
        instance: {
          moa_page_name: _.find(that.menus, { id: that.selectedPage }).component
        }
      })
    });

  }

  onSegmentChange(item) {
    console.log(item);
    let that = this;
    this.selectedPage = item.id;
    this.common.addWatermark({
      instance: {
        moa_page_name: item.component
      }
    });
    this.storage.set("selected-home-page", item.id);
    this.common.saveUserStatistics(item.component, "", item.name);
  }

}
